package org.genntii.warehousesystem.handler;

import lombok.extern.slf4j.Slf4j;
import org.genntii.warehousesystem.Exception.BaseException;
import org.genntii.warehousesystem.common.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    // 业务异常
    @ExceptionHandler(value = BaseException.class)
    public Result<String> bizExceptionHandler(BaseException e){
        log.error("业务异常");
        return Result.error(e.getMessage());
    }

    // 空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    public Result<String> NullPointExceptionHandler(NullPointerException e){
        log.error("空指针异常");
        return Result.error("空指针异常："+e.getMessage());
    }

    // 服务器异常
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> ServerExceptionHandler(Exception e){
        log.error("服务端异常");
        return Result.error(e.getMessage());
    }

}
